﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.IClasssB;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System.Collections.Generic;

namespace StartsEAS_API.Controllers.ClassContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class ClassController : ControllerBase
    {
        private readonly IClassBLL _classBll;
        public ClassController(IClassBLL classBll)
        {
            _classBll = classBll;
        }
        /// <summary>
        /// 根据班级编号查询班级信息，一般为教务处使用
        /// </summary>
        /// <param name="ClassId">班级编号</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetClassesByClassId(string ClassId) {
            try
            {
                return _classBll.GetClassesByClassId(ClassId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetClassesByClassId服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据token查询班级信息
        /// </summary>
        /// <param name="token">老师，学生token</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetClassesByToken(string token) {
            try
            {
                return _classBll.GetClassesByToken(token);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetClassesByToken服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 添加班级信息
        /// </summary>
        /// <param name="token">教务处token</param>
        /// <param name="classes">班级信息</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils AddClasses(string token, Class classes) {
            try
            {
                return _classBll.AddClasses(token, classes);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"AddClasses服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 修改班级信息
        /// </summary>
        /// <param name="token">教务处token</param>
        /// <param name="data">班级信息</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils UpdateClasses(string token, Class data) {
            try
            {
                return _classBll.UpdateClasses(token, data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"UpdateClasses服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 查询所有班级
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllClasses() {
            try
            {
                return _classBll.GetAllClasses();
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllClasses服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }

        [HttpGet]
        public ResultUtils GetAllClassesByProfessionalId(int ProfessionalId)
        {
            try
            {
                return _classBll.GetAllClassesByProfessionalId(ProfessionalId);
            }
            catch (System.Exception e) {
                AppLog.WriteError($"GetAllClassesByProfessionalId服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
